﻿using UnityEngine;
using System.Collections;

public class EventMgr : MonoBehaviour {

	GameObject MyEvent;

	// Use this for initialization
	void Start () {
		MyEvent = Resources.Load ("Prefaps/UI/StaticUI/Event/TruckEvent") as GameObject;
        //TruckEventing();
        StartCoroutine(TruckTimer(0.0f));
        StartCoroutine(TimedEventTimer(Random.Range(130.0f, 160.0f)));
	}
	
	void TruckEventing(){
		int iChildCount = transform.childCount;

        switch (Random.Range (0, 3)) 
        //int j =0   ;
        //switch (j)
        {
		case 0:
			(Instantiate(MyEvent) as GameObject).transform.parent = transform;
            transform.GetChild(iChildCount).GetComponent<TruckEvent>().EventID = TRUCK_EVENT.REINFORCE;
			break;
		case 1:
			(Instantiate(MyEvent) as GameObject).transform.parent = transform;
            transform.GetChild(iChildCount).GetComponent<TruckEvent>().EventID = TRUCK_EVENT.AMMO;
			break;
		case 2:
			(Instantiate(MyEvent) as GameObject).transform.parent = transform;
            transform.GetChild(iChildCount).GetComponent<TruckEvent>().EventID = TRUCK_EVENT.SUPPLY;
			break;
		}
	}

    void CreateEventTimer()
    {   
        int iChildCount = transform.childCount;

        //switch (Random.Range (3, 5)) {
        int j = 4;
        switch (j) {
            case 0:
                ResourceMgr.GetInstance().CreateTimedEvent("RiotComing");
                break;

            case 1:
                ResourceMgr.GetInstance().CreateTimedEvent("Retreat");
                break;

            case 2:
                ResourceMgr.GetInstance().CreateTimedEvent("Attack");
                break;

            case 3:
                ResourceMgr.GetInstance().CreateTimedEvent("SupplyBeCutOff");
                break;

            case 4:
                ResourceMgr.GetInstance().CreateTimedEvent("Volunteer");
                break;
        }
    }

	IEnumerator TruckTimer(float fTime){
        yield return new WaitForSeconds(fTime);
        TruckEventing();
	}

    IEnumerator TimedEventTimer(float fTime)
    {
        yield return new WaitForSeconds(fTime);
        CreateEventTimer();
    }

    

    public void SetTruckEventTimer(float fTime = 0) // 매개변수없으면 랜덤시간
    {
        if (fTime == 0.0f)
            fTime = Random.Range(30.0f, 60.0f);

        StopCoroutine("TruckTimer");
        StartCoroutine("TruckTimer", fTime);
    }

    public void SetTimedEventTimer()
    {
        StartCoroutine(TimedEventTimer(Random.Range(130.0f, 160.0f)));
    }

    public void EventMsgSend(float fTime, string strEventID)
    {
        StartCoroutine(EventMsgSend_Coroutine(fTime, strEventID));
    }

    public IEnumerator EventMsgSend_Coroutine(float fTime, string strEventID)
    {
        yield return new WaitForSeconds(fTime);

        ResourceMgr.GetInstance().CreateEventMsg(strEventID);
    }
}
